package pert;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class PertToDot {
	public boolean convertPertToDot(String inputFilePath) {
		BufferedReader br = null;
		String line = "";
		String splitBy = ",";
		List<Task> tasks = new ArrayList<Task>();

		try {
			br = new BufferedReader(new FileReader(inputFilePath));
			while ((line = br.readLine()) != null) {
				String[] lineParts = line.replace(" ", "").split(splitBy);
				Task taskObject = new Task(lineParts[0],
						Integer.parseInt(lineParts[1]));
				for (int i = 2; i < lineParts.length; i++) {
					taskObject.getPreceedingTasks().add(lineParts[i]);
				}
				tasks.add(taskObject);
			}
			createDotFile(tasks, new File(new File(inputFilePath).getParent(),
					"output.dot"));
			br.close();
		} catch (Exception e) {
			return false;
		}
		int noDependenciesCount = 0;
		for (int i = 0; i < tasks.size(); i++) {
			if (tasks.get(i).preceedingTasks.isEmpty()) {
				noDependenciesCount++;
			}
		}
		if (noDependenciesCount > 1) {
			return false;
		}

		return true;
	}

	static void createDotFile(List<Task> taskList, File outputDotFilePath) {
		try {
			FileWriter fw = new FileWriter(outputDotFilePath);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write("digraph dot {\n");
			for (int i = 0; i < taskList.size(); i++) {
				for (int j = 0; j < taskList.get(i).getPreceedingTasks().size(); j++) {
					bw.write(taskList.get(i).getPreceedingTasks().get(j)
							+ " -> " + taskList.get(i).getName() + ";\n");
				}
			}

			bw.write("}");
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Dotfile created");
	}

	public List<Task> convertCsvToClass(String inFile) {
		String csvToRead = inFile;
		BufferedReader br = null;
		String line = "";
		String splitBy = ",";
		List<Task> taskList = new ArrayList<Task>();

		try {
			br = new BufferedReader(new FileReader(csvToRead));
			while ((line = br.readLine()) != null) {
				String[] tasks = line.split(splitBy);

				Task taskObject = new Task();
				taskObject.setName(tasks[0]);
				taskObject.setDuration(Integer.parseInt(tasks[1]));

				String[] dependencies = new String[tasks.length - 2];
				for (int i = 2; i < tasks.length; i++) {
					dependencies[i - 2] = tasks[i];
				}
				taskObject.setDependenciesReference(dependencies);
				taskList.add(taskObject);
			}
			for (int i = 0; i < taskList.size(); i++) {
				Task taskObject = taskList.get(i);
				String[] dependencies = taskObject.getDependenciesReference();

				for (int j = 0; j < dependencies.length; j++) {
					String taskName = dependencies[j];

					boolean found = false;
					int m = 0;
					while (!found && m < taskList.size()) {
						Task taskToFind = taskList.get(m);
						if (taskToFind.getName().compareTo(taskName) == 0) {
							found = true;
							taskObject.getDependencies().add(taskToFind);
						}
						m++;
					}

				}
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

}